define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lihua_bonus_act/index' + location.search,
                    add_url: 'lihua_bonus_act/add',
                    edit_url: 'lihua_bonus_act/edit',
                    del_url: 'lihua_bonus_act/del',
                    multi_url: 'lihua_bonus_act/multi',
                    table: 'lihua_bonus_act',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'type', title: __('Type'), visible: false},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', formatter : Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange',  formatter : Table.api.formatter.datetime},
                        {field: 'status', title: "状态", formatter: function(row) {
                                return !!row?"正常":"禁用";
                            }
                        },
                        {field: 'city_id', title: __('City_id'), formatter: function(row) {
                                if(row==1){return "北京"};
                                if(row==2){return '上海'};
                                if(row==3){return '常州'};
                                if(row==4){return '南京'};
                                if(row==5){return '苏州'};
                                if(row==6){return '无锡'};
                                if(row==7){return '广州'};
                            }
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            setTimeout(()=>{
                var v = $('input:radio[name="row[bonus_type]"]:checked').val();
                v++;
                $(".bonus_type_s").hide();
                $(".bonus_type_"+v).show();
            }, 2000);
            
            $('input:radio[name="row[bonus_type]"]').click(function() {
                var v = $('input:radio[name="row[bonus_type]"]:checked').val();
                v++;
                $(".bonus_type_s").hide();
                $(".bonus_type_"+v).show();
            });

            Controller.api.bindevent();
        },
        edit: function () {
            setTimeout(()=>{
                var v = $('input:radio[name="row[bonus_type]"]:checked').val();
                v++;
                $(".bonus_type_s").hide();
                $(".bonus_type_"+v).show();
            }, 2000);

            $('input:radio[name="row[bonus_type]"]').click(function() {
                var v = $('input:radio[name="row[bonus_type]"]:checked').val();
                v++;
                $(".bonus_type_s").hide();
                $(".bonus_type_"+v).show();
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});